/*
 * Copyright 2010 Askar Kalykov, oxygene.core@gmail.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.oxygenecore.processor.expression.logical;

import com.oxygenecore.processor.CalculationException;
import com.oxygenecore.processor.ExecutionContext;
import com.oxygenecore.processor.Expression;
import com.oxygenecore.processor.UnsupportedExpressionException;
import com.oxygenecore.processor.expression.logical.ComparisionExpression;

/**
 * @author Askar Kalykov
 *         Date: 11.09.2010
 *         Time: 18:33:39
 */
abstract public class AbstractIntegerCompareExpression implements ComparisionExpression {
    private Expression<Integer> leftArgument;
    private Expression<Integer> rightArgument;
    private Operation operation;

    public AbstractIntegerCompareExpression(Expression<Integer> leftArgument, Expression<Integer> rightArgument, Operation operation){
        if (leftArgument == null){
            throw new NullPointerException("leftArgument");
        }
        if (rightArgument == null){
            throw new NullPointerException("rightArgument");
        }
        if (operation == null){
            throw new NullPointerException("operation");
        }

        this.leftArgument = leftArgument;
        this.rightArgument = rightArgument;
        this.operation = operation;
    }

    public Boolean getValue(ExecutionContext executionContext) throws CalculationException {
        Integer left = leftArgument.getValue(executionContext);
        Integer right = rightArgument.getValue(executionContext);
        switch (operation) {
            case LESS: return left < right;
            case LESS_OR_EQUALS: return left <= right;
            case EQUALS: return left.equals(right);
            case MORE_OR_EQUALS: return left >= right;
            case MORE: return left > right;
            default: throw new UnsupportedExpressionException(this);
        }
    }

}
